package com.wxq.workpad.finishproduct

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import com.samluys.statusbar.StatusBarUtils
import com.wxq.workpad.base.BaseActivity
import com.wxq.workpad.base.RealShow
import com.wxq.workpad.databinding.ActivityMainBinding
import com.wxq.workpad.entity.SaiXuanItem
import com.wxq.workpad.lailiao.adapter.SaiXuanListAdapter

import com.wxq.workpad.lailiao.LaiLiaoFragmentAdapter
import com.wxq.workpad.lailiao.dialog.ChooseSaiXuanDialog
import com.wxq.workpad.lailiao.dialog.ChooseSaiXuanDialogOld


class ProductListActivity : BaseActivity<ActivityMainBinding>() {


    private lateinit var fragments: MutableList<Fragment>
    private lateinit var tabTitles: MutableList<String>
    private lateinit var saiXuanListAdapter: SaiXuanListAdapter

    lateinit var chooseSaiXuanDialogOld: ChooseSaiXuanDialog

    private var saiXuanDialogList: MutableList<SaiXuanItem>

    // 提供给recycleview 的数据
    private var saiXuanListWithInput: MutableList<SaiXuanItem>


    init {
        saiXuanDialogList = ArrayList()
        saiXuanListWithInput = ArrayList()

        saiXuanDialogList.add(SaiXuanItem("到货单号", 1))
        saiXuanDialogList.add(SaiXuanItem("供方", 1))
        saiXuanDialogList.add(SaiXuanItem("编码", 1))
        saiXuanDialogList.add(SaiXuanItem("名称", 1))
        saiXuanDialogList.add(SaiXuanItem("厂家", 1))
        saiXuanDialogList.add(SaiXuanItem("图号", 1))
        saiXuanDialogList.add(SaiXuanItem("类型", 2))
        saiXuanDialogList.add(SaiXuanItem("来源", 2))
        saiXuanDialogList.add(SaiXuanItem("日期", 3))
    }


    override fun initData() {

    }

    override fun initView(instanceState: Bundle?) {
        StatusBarUtils.transparencyBar(this@ProductListActivity)
        StatusBarUtils.StatusBarIconDark(this@ProductListActivity)
        tabTitles = ArrayList()
        tabTitles.add("全部")
        tabTitles.add("待检验")
        tabTitles.add("已检验")
        fragments = ArrayList()
        fragments.add(ProductListFragment.newInstance(""))
        fragments.add(ProductListFragment.newInstance("1"))
        fragments.add(ProductListFragment.newInstance("2"))
        binding.viewpager.offscreenPageLimit = 3
        binding.viewpager.adapter =
            LaiLiaoFragmentAdapter(supportFragmentManager, fragments, tabTitles)
        binding.viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                if (fragments.get(position) is RealShow) {
                    (fragments.get(position) as RealShow).realShow()
                }
            }

            override fun onPageScrollStateChanged(state: Int) {

            }

        })
        binding.tabLayout.setTabMode(TabLayout.MODE_FIXED)
        binding.tabLayout.setupWithViewPager(binding.viewpager)
        binding.tabLayout.setTabsFromPagerAdapter(binding.viewpager.adapter)


        binding.viewpager.setCurrentItem(0)
        if (fragments.get(0) is RealShow) {
            (fragments.get(0) as RealShow).realShow()
        }
        binding.ivBack.setOnClickListener {
            onBackPressed()
        }

        binding.ivSaixuan.setOnClickListener {
            showToast("显示筛选内容")


            chooseSaiXuanDialogOld.showDialog(saiXuanDialogList)
        }
        chooseSaiXuanDialogOld =
            ChooseSaiXuanDialog(mContext)
        chooseSaiXuanDialogOld.setListener(object : ChooseSaiXuanDialog.ClickItemListener {
            override fun click(position: Int) {

            }

            override fun getConfirmData(data: MutableList<SaiXuanItem>?) {
                if (data != null) {
                    saiXuanListWithInput.addAll(data)
                    saiXuanListAdapter.updateDate(saiXuanListWithInput)
                    saiXuanDialogList.removeAll(data)
                    chooseSaiXuanDialogOld.updateUI()
                }
            }
        })

        saiXuanListAdapter= SaiXuanListAdapter(mContext)
        binding.rvSaixuan.layoutManager=LinearLayoutManager(mContext!!, LinearLayoutManager.VERTICAL, false)
        binding.rvSaixuan.adapter=saiXuanListAdapter
        saiXuanListAdapter.setRemoveItemListener {
            saiXuanDialogList.add(it)
            chooseSaiXuanDialogOld.updateUI()
        }

    }



    companion object {

        @JvmStatic
        fun newInstance(context: Context) {
            val intent = Intent(context, ProductListActivity::class.java)
            context.startActivity(intent)
        }
    }

}